import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 创建user对象
        User user = new User();
        user.setUsercode("111");
        user.setUsername("zhangsan");

        // 将user存储在request范围中
        request.setAttribute("userObj", user);

//        // 从request范围中取出数据
//        Object obj = request.getAttribute("userObj");
//        response.getWriter().print(obj);

        // 跳转
        // 执行完AServlet之后, 跳转到BServlet执行, 将AServlet执行和BServlet执行放到一个请求中
        // 必须使用转发技术
        // forward [转发]
        // 1. 获取请求转发器对象
        // request.getRequestDispatcher("/路径") 获取请求转发器
        // 以下转发器指向了"/b"资源
        RequestDispatcher dispatcher = request.getRequestDispatcher("/b");
        // 2. 调用请求转发器的forward方法即可完成转发
        dispatcher.forward(request, response);

    }
}
